from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
from user.models import User


def index_handler(request):
    return HttpResponse('django index')


def set_cookie_handler(request, key, value):
    response = HttpResponse()
    response.set_cookie(key, value, max_age=60 * 60)
    return response


def get_cookie_handler(request,key):
    value= request.COOKIES.get(key)
    return HttpResponse(value)


def set_session_handler(request,key,value):
    request.session[key]=value
    return HttpResponse('设置成功')


def get_session_handler(request,key):
    value = request.session.get(key)
    return HttpResponse(value)


def clear_handler(request):
    request.session.clear()
    return HttpResponse('clear')
def flush_handler(request):
    request.session.flush()
    return HttpResponse('flush')


def form_handler(request):
    return render(request,'form.html')

def form_get_hangler(request):
    username = request.GET.get('username')
    hobbys = request.GET.getlist('hobby')
    print("username=",username)
    print('hoobys=',hobbys)
    return HttpResponse("")

def form_post_hangler(request):
    username = request.POST.get('username')
    hobbys = request.POST.getlist('hobby')
    print("username=", username)
    print('hoobys=', hobbys)
    return HttpResponse("")


def regist_hangler(request):
    if request.method=='GET':
        return render(request,'regist.html')
    else:
        username = request.POST.get("username")
        password = request.POST.get('password')
        User(username=username,password=password).save()
        return HttpResponse('注册成功')
